//
//  WeiboContent.swift
//  WeiBo test
//
//  Created by Aquarius_wing on 16/5/7.
//  Copyright © 2016年 Wing. All rights reserved.
//

import UIKit
import AFNetworking

class WeiboContent: NSObject {
    var name:String?
    var text:String?
    var time:TimeModel?
    var source:NSURL?
    
    var user:NSDictionary?{
        didSet{
            userInfo=UserInfo(dict: user as! [String : AnyObject])
        }
    }
    
    var id:NSNumber?
    var userId:NSNumber?
    var userInfo:UserInfo?{
        didSet{
            name=userInfo?.name
            avatar_large=userInfo?.avatar_large
            userId=userInfo?.id
        }
    }
    var avatar_large:String?
    var headImageURL: NSURL? {
        return NSURL(string: avatar_large ?? "")
    }
    var height:CGFloat=0
    var created_at:NSDate?{
        didSet{
            let t=TimeModel.init(date:created_at!)
            time=t
        }
    }
    //这里图片地址肯定会被获取到,不会为nil
    var pic_urls:[[String : String]]?
    
    var imageURLs:[NSURL]?{
        guard let urls = pic_urls else {
            return nil
        }
        
        var tempArray = [NSURL]()
        for item in urls {
            //相信后端给的urlString 一定能够转换成 url
            let url = NSURL(string: item["thumbnail_pic"]!)
            tempArray.append(url!)
        }
        return tempArray
    }
    
    init(dict : [String:AnyObject]) {
        super.init()
        print("***",dict,"***")
        setValuesForKeysWithDictionary(dict)
        print(self)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    //要使打印该类为字符串
    override var description: String{
        let keys=["name","text","userInfo","created_at","pic_urls","id","userId","source"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
}
extension String {
    
    var html2AttributedString: NSAttributedString? {
        guard
            let data = dataUsingEncoding(NSUTF8StringEncoding)
            else { return nil }
        do {
            return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil)
        } catch let error as NSError {
            print(error.localizedDescription)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
